﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Enterprise.Infrastructure;
using System.IO;
using System.Configuration;

namespace Enterprise.UI.Web.Modules.WebEmail
{
    public partial class emailDetail : System.Web.UI.Page
    {
        public System.Data.DataSet Email { get; set; }

        public int AttchCount { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            Enterprise.Service.Email.EmailService es  = new Service.Email.EmailService();
            string messageId = (string)Utility.sink("id", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
            object id = Request.QueryString["id"];
            Email = es.getMailDetail(messageId);
            es.SetReaded(messageId);
            string str = GetFeild("EmailAttachMENTS");
            if (string.IsNullOrEmpty(str))
                AttchCount = 0;
            else
                AttchCount = str.Split('|').Count();
        }

        public string GetFeild(string filed)
        {
            string str = "";
            if (Email.Tables[0].Rows.Count > 0)
            {
                str= Email.Tables[0].Rows[0][filed].ToString();
            }
            return str;
        }

        public string AttachMentDownUrl(string attchStr)
        {
            Enterprise.Infrastructure.CryptographerHelper cry = new CryptographerHelper();            
            string html = "<ul>";
            if (!string.IsNullOrEmpty(attchStr))
            {
                string[] str = attchStr.Split('|');
                //Hack:将str输出的客户端
                foreach (var file in str)
                {
                    string file_des = cry.Encrypt(file);//des加密
                    string url = System.Web.HttpContext.Current.Server.UrlEncode(file_des);//URL编码
                    if (File.Exists(ConfigurationManager.AppSettings["EmailAttachmentsPath_Inbox"] + file))
                        html += "<li><a href='downattch.aspx?p=" + url + "'>" + Path.GetFileName(file) + "</a></li>";
                }
            }
            return html + "</ul>";
        }
    }
}